package com.xintool.window.Buttons;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

public class Button_characteristic extends ButtonFactory {
    private JFrame frame;
    private final LinkedHashMap<String, String> FEATURE_MAP = new LinkedHashMap<>();
    private final Map<String, List<String>> CATEGORIES = new LinkedHashMap<>();


    @Override
    public JButton showButton() {
        JButton button = ButtonFactory.getButton("特性表");
        button.addActionListener(e -> createMainWindow());
        initFeatureData();
        return button;
    }

    // 初始化特性数据和分类
    private void initFeatureData() {
        // 特性
        FEATURE_MAP.put("瞬杀", "进攻类技能有3%几率秒杀对方");
        FEATURE_MAP.put("黑夜", "暗影属性技能伤害增加5%");
        FEATURE_MAP.put("光环", "光属性技能伤害增加5%");
        FEATURE_MAP.put("奇异", "神秘属性技能伤害增加5%");
        FEATURE_MAP.put("威严", "龙属性技能伤害增加5%");
        FEATURE_MAP.put("会心", "所有技能的致命一击率增加1/16");
        FEATURE_MAP.put("顽强", "受到致死攻击时有3%几率余下1点体力");
        FEATURE_MAP.put("回神", "精灵体力降低到1/8时有3%几率体力回满");
        FEATURE_MAP.put("坚硬", "受到任何伤害减少5%");
        FEATURE_MAP.put("高热", "受到普通攻击时有3%使对方烧伤5%");
        FEATURE_MAP.put("反抗", "受到特殊攻击时有5%几率使对方攻击降低1个等级");
        FEATURE_MAP.put("反击", "受到任何攻击时有5%几率使自身攻击提升1个等级");
        FEATURE_MAP.put("蓄电", "电属性技能伤害增加5%");
        FEATURE_MAP.put("碎裂", "地面属性技能伤害增加5%");
        FEATURE_MAP.put("冰霜", "冰属性技能伤害增加5%");
        FEATURE_MAP.put("机能", "机械属性技能伤害增加5%");
        FEATURE_MAP.put("魔幻", "超能属性技能伤害增加5%");
        FEATURE_MAP.put("冰冷", "受到普通攻击时有3%几率使对方冻伤");
        FEATURE_MAP.put("忽略", "受到特殊攻击时有5%几率使对方特工降低1个等级");
        FEATURE_MAP.put("抵抗", "受到任何攻击时有5%几率使自身防御提升1个等级");
        FEATURE_MAP.put("战意", "战斗属性技能伤害增加5%");
        FEATURE_MAP.put("平衡", "普通属性技能伤害增加5%");
        FEATURE_MAP.put("草率", "受到特殊攻击时有5%几率使对方特防降低1个等级");
        FEATURE_MAP.put("反攻", "受到任何攻击时有5%几率使自身特工提升1个等级");
        FEATURE_MAP.put("精准", "所有技能命中率增加5%");
        FEATURE_MAP.put("回避", "被技能命中的几率减少5%");
        FEATURE_MAP.put("坚韧", "受到任何攻击时有5%几率使自身特防提升1个等级");
        FEATURE_MAP.put("反驳", "受到特殊攻击时有5%几率使对方防御降低1个等级");
        FEATURE_MAP.put("叶绿", "草属性技能伤害增加5%");
        FEATURE_MAP.put("流水", "水属性技能伤害增加5%");
        FEATURE_MAP.put("炎火", "火属性技能伤害增加5%");
        FEATURE_MAP.put("飞空", "飞行属性技能伤害增加5%");
        FEATURE_MAP.put("阴森", "受到普通攻击时3%几率使对方害怕");
        FEATURE_MAP.put("带电", "受到普通攻击时3%几率使对方麻痹");
        FEATURE_MAP.put("慌张", "受到特殊攻击时有5%几率使对方速度降低1个等级");
        FEATURE_MAP.put("借风", "受到任何攻击时有5%几率使自身速度提升1个等级");
        FEATURE_MAP.put("吸收", "受到攻击时5%几率使受到的伤害降低35点");
        FEATURE_MAP.put("虚无", "3%几率完全抵挡一次伤害");
        FEATURE_MAP.put("强攻", "物理攻击有5%几率使伤害提高35点");
        FEATURE_MAP.put("强念", "特殊攻击有5%几率使伤害提高35点");
        FEATURE_MAP.put("免爆", "受到致命一击的概率降低1/16");
        FEATURE_MAP.put("乱舞", "群体攻击技能可额外增加一个目标");
        FEATURE_MAP.put("强袭", "物理攻击伤害增加3%");
        FEATURE_MAP.put("精神", "特殊攻击伤害增加3%");
        FEATURE_MAP.put("火热", "自身的特殊攻击有3%几率使对方烧伤");
        FEATURE_MAP.put("极寒", "自身的特殊攻击有3%几率使对方冻伤");
        FEATURE_MAP.put("颤栗", "自身的物理攻击有3%几率使对方害怕");
        FEATURE_MAP.put("静电", "自身的物理攻击有3%几率使对方麻痹");

        // 分类
        CATEGORIES.put("属性增伤", Arrays.asList(
                "黑夜", "光环", "奇异", "威严", "蓄电", "碎裂",
                "冰霜", "机能", "魔幻", "战意", "平衡", "叶绿",
                "流水", "炎火", "飞空"));

        CATEGORIES.put("防御强化", Arrays.asList(
                "回神", "顽强", "坚硬", "免爆", "回避", "吸收", "虚无", "抵抗", "坚韧", "反驳",
                "反抗","反击","反攻","草率", "忽略", "慌张","借风"));

        CATEGORIES.put("攻击强化", Arrays.asList(
                "瞬杀","会心", "精准", "强袭", "精神", "强攻", "强念",
                "乱舞"));

        CATEGORIES.put("状态附加", Arrays.asList(
                "高热", "冰冷", "阴森", "带电",
                "火热", "极寒", "颤栗", "静电"));
    }

    private void createMainWindow() {
        if (frame != null) {
            frame.toFront();
            return;
        }

        frame = new JFrame("特性表(鼠标悬停查看)");
        frame.setSize(395, 385);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 改用垂直盒子布局作为主容器
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 添加分类区块
        CATEGORIES.forEach((category, features) -> {
            mainPanel.add(createCategoryPanel(category, features));
            mainPanel.add(Box.createRigidArea(new Dimension(0, 10))); // 分类间距
        });

        frame.add(mainPanel);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                frame = null;
            }
        });
        frame.setVisible(true);
    }

    // 创建分类面板（水平布局）
    private JPanel createCategoryPanel(String title, List<String> features) {
        JPanel panel = new JPanel(new BorderLayout());

        // 带间距的标题边框
        TitledBorder border = BorderFactory.createTitledBorder(title);
        border.setTitleFont(new Font("微软雅黑", Font.BOLD, 13));
        panel.setBorder(border);

        // 使用自动换行面板
        JPanel content = new JPanel(new WrapLayout(FlowLayout.LEFT, 10, 5));
        features.forEach(f -> content.add(createFeatureLabel(f)));

        panel.add(content, BorderLayout.CENTER);
        return panel;
    }

    // 创建特性标签（带悬停提示）
    private JLabel createFeatureLabel(String name) {
        JLabel label = new JLabel(name);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        label.setToolTipText(createTooltipText(name));

        // 鼠标悬停效果
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                label.setForeground(new Color(0, 100, 200));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                label.setForeground(Color.BLACK);
            }
        });

        return label;
    }

    // 创建格式化提示信息
    private String createTooltipText(String name) {
        return "<html><div style='width: 180px; padding: 5px;'>" +
                "<b>" + name + "</b><br>" +
                FEATURE_MAP.get(name) + "</div></html>";
    }
    static class WrapLayout extends FlowLayout {
        public WrapLayout(int align, int hgap, int vgap) {
            super(align, hgap, vgap);
        }

        @Override
        public Dimension preferredLayoutSize(Container target) {
            return layoutSize(target, true);
        }

        private Dimension layoutSize(Container target, boolean preferred) {
            synchronized (target.getTreeLock()) {
                int width = target.getWidth();
                if (width == 0) width = Integer.MAX_VALUE;

                int height = getVgap();
                int rowWidth = 0;
                int rowHeight = 0;

                for (Component comp : target.getComponents()) {
                    Dimension dim = preferred ? comp.getPreferredSize() : comp.getMinimumSize();
                    if (rowWidth + dim.width + getHgap() > width) {
                        height += rowHeight + getVgap();
                        rowWidth = 0;
                        rowHeight = 0;
                    }
                    rowWidth += dim.width + getHgap();
                    rowHeight = Math.max(rowHeight, dim.height);
                }
                height += rowHeight;
                return new Dimension(width, height);
            }
        }
    }
}

